home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / TreeNode.java < prev    next >
Text File  |  1998-08-21  |  9KB  |  379 lines

  1. package symantec.itools.awt;
  2.  
  3.  
  4. import java.awt.Image;
  5.  
  6.  
  7. //    02/10/97    RKM    Added data object
  8. //    02/27/97    RKM    Merged in accessors for getParent, getChild, & getSibling
  9. //  07/25/97    CAR marked fields transient as needed will have to re-test after event handling code is changed
  10. //                  implements interface java.io.Serializable
  11. //  08/20/97    LAB Added dependency on having a reference to the containing TreeView instance.
  12. //                    This modified the constructors, and deprecated the existing constructors.
  13. //                    TreeNode now calls TreeView's triggerRedraw when it needs to refresh its
  14. //                    visible state.  Addresses Mac Bug #4372
  15. //  08/21/97    LAB Actually un-commented the fix for calling triggerRedraw (oy!).
  16. //  02/09/98    DS  Added support for hiding/showing nodes (TreeNode.setHidden(boolean)
  17.  
  18.  
  19. /**
  20.  * This is a single node in the TreeView panel.
  21.  * It displays text and optionally one of two images depending on its state,
  22.  * collapsed or expanded.
  23.  * It also may have an object associated with it that doesn't get displayed.
  24.  * @see TreeView
  25.  */
  26. public class TreeNode implements java.io.Serializable
  27. {
  28.     TreeNode    sibling;
  29.     TreeNode    child;
  30.     TreeNode    parent;
  31.     String        text;
  32.     transient Image        collapsedImage;
  33.     transient Image        expandedImage;
  34.     int            numberOfChildren;
  35.     Object        dataObject;
  36.     TreeView    treeView;
  37.  
  38.     int            depth = -1;
  39.     boolean    isExpanded = false;
  40.     boolean hidden;
  41.  
  42.     //constructors
  43.  
  44.     /**
  45.      * Constructs a default TreeNode.
  46.      */
  47.     public TreeNode() { }
  48.  
  49.     /**
  50.      * @deprecated
  51.      * @see TreeNode#TreeNode(java.lang.String, symantec.itools.awt.TreeView)
  52.      */
  53.     public TreeNode(String text)
  54.     {
  55.         this(text, null, null, null);
  56.     }
  57.  
  58.     /**
  59.      * @deprecated
  60.      * @see TreeNode#TreeNode(java.lang.String, java.awt.Image, java.awt.Image, symantec.itools.awt.TreeView)
  61.      */
  62.     public TreeNode(String text, Image collapsedImage, Image expandedImage)
  63.     {
  64.         this(text, collapsedImage, expandedImage, null);
  65.     }
  66.  
  67.     /**
  68.      * Constructs a TreeNode with the given text label.
  69.      * @param text the text to display for this node
  70.      * @param treeView the instance of TreeView whose node this is.
  71.      * Typically "this"
  72.      */
  73.     public TreeNode(String text, TreeView treeView)
  74.     {
  75.         this(text, null, null, treeView);
  76.     }
  77.  
  78.     /**
  79.      * Constructs a TreeNode with the given text label, and collapsed and
  80.      * expanded images.
  81.      * @param text the text to display for this node
  82.      * @param collapsedImage the image to use when this node is collapsed, hiding
  83.      * all of its child nodes
  84.      * @param expandedImage the image to use when this node is expanded, showing
  85.      * all of its child nodes
  86.      * @param treeView the instance of TreeView whose node this is.
  87.      * Typically "this"
  88.      */
  89.     public TreeNode(String text, Image collapsedImage, Image expandedImage, TreeView treeView)
  90.     {
  91.         this.text                = text;
  92.         this.sibling            = null;
  93.         this.child                = null;
  94.         this.collapsedImage     = collapsedImage;
  95.         this.expandedImage      = expandedImage;
  96.         this.numberOfChildren    = 0;
  97.         this.dataObject            = null;
  98.         this.treeView            = treeView;
  99.     }
  100.  
  101.     /**
  102.      * Notes the current depth of this node.
  103.      * @param depth
  104.      * @see #getDepth
  105.      */
  106.     void setDepth(int depth)
  107.     {
  108.         this.depth = depth;
  109.     }
  110.  
  111.     /**
  112.      * Gets the depth of this node as previously noted.
  113.      * @return the depth of this node
  114.      */
  115.     public int getDepth()
  116.     {
  117.         return depth;
  118.     }
  119.  
  120.     /**
  121.      * Determines whether this node is expanded.
  122.      * A node is expanded if its child nodes are visible.
  123.      * @return true if the node is expanded, false if it is collapsed
  124.      */
  125.     public boolean isExpanded()
  126.     {
  127.         return isExpanded;
  128.     }
  129.  
  130.     /**
  131.      * Determines whether this node is expandable.
  132.      * A node is expandable if it has one or more child nodes.
  133.      * @return true if the node is expandable, false if not
  134.      */
  135.     public boolean isExpandable()
  136.     {
  137.         return (child!=null);
  138.     }
  139.  
  140.     /**
  141.      * Sets a flag indicating that this node is expanded, if it is expandable.
  142.      */
  143.     public void expand()
  144.     {
  145.         if (isExpandable())
  146.         {
  147.             isExpanded=true;
  148.             if (treeView != null)
  149.                 treeView.triggerRedraw();
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Sets a flag indicating that this node is not expanded.
  155.      */
  156.     public void collapse()
  157.     {
  158.         isExpanded = false;
  159.         if (treeView != null)
  160.             treeView.triggerRedraw();
  161.     }
  162.  
  163.     /**
  164.      * Toggles the node state between collapsed and expanded, if the node
  165.      * is expandable.
  166.      */
  167.     public void toggle()
  168.     {
  169.         if (isExpanded)
  170.         {
  171.             collapse();
  172.         }
  173.         else if (isExpandable())
  174.         {
  175.             expand();
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * Gets the proper image for this node in its current state, expanded or collapsed.
  181.      * @return the current image for this node in its current state
  182.      */
  183.     public Image getImage()
  184.     {
  185.         return ((isExpanded && (expandedImage != null))
  186.                 ? expandedImage
  187.                 : collapsedImage);
  188.     }
  189.  
  190.     /**
  191.      * Sets the image to use for this node when it is expanded.
  192.      * @param image the image to use when this node is expanded
  193.      * @see #setCollapsedImage
  194.      * @see #getImage
  195.      */
  196.     public void setExpandedImage(Image image)
  197.     {
  198.         expandedImage = image;
  199.         if (isExpanded() && treeView != null)
  200.             treeView.triggerRedraw();
  201.     }
  202.  
  203.     /**
  204.      * Sets the image to use for this node when it is not expanded.
  205.      * @param image the image to use when this node is collapsed
  206.      * @see #setExpandedImage
  207.      * @see #getImage
  208.      */
  209.     public void setCollapsedImage(Image image)
  210.     {
  211.         collapsedImage = image;
  212.         if (!isExpanded() && treeView != null)
  213.             treeView.triggerRedraw();
  214.     }
  215.  
  216.     /**
  217.      * Gets the current text label for this node.
  218.      * @return the current text label for this node
  219.      * @see #setText
  220.      */
  221.     public String getText()
  222.     {
  223.         return text;
  224.     }
  225.  
  226.     /**
  227.      * Sets a new text label for this node.
  228.      * @param s the new text label for this node
  229.      * @see #getText
  230.      */
  231.     public void setText(String s)
  232.     {
  233.         text = new String(s);
  234.         if (treeView != null)
  235.             treeView.triggerRedraw();
  236.     }
  237.  
  238.     /**
  239.      * Gets the object associated with this node.
  240.      * This object does not get displayed.
  241.      * @return the object associated with this node
  242.      * @see #setDataObject
  243.      */
  244.     public Object getDataObject()
  245.     {
  246.         return dataObject;
  247.     }
  248.  
  249.     /**
  250.      * Sets an object to associate with this node.
  251.      * This object does not get displayed.
  252.      * @param theObject an object to associate with this node
  253.      * @see #getDataObject
  254.      */
  255.     public void setDataObject(Object theObject)
  256.     {
  257.         dataObject = theObject;
  258.     }
  259.  
  260.     /**
  261.      * Gets the parent of this node.
  262.      * @return this node's parent node
  263.      * @see #getChild
  264.      * @see #getSibling
  265.      */
  266.     public TreeNode getParent()
  267.     {
  268.         return parent;
  269.     }
  270.  
  271.     /**
  272.      * Gets the child of this node.
  273.      * @return this node's child node
  274.      * @see #getParent
  275.      * @see #getSibling
  276.      */
  277.     public TreeNode getChild()
  278.     {
  279.         return child;
  280.     }
  281.  
  282.     /**
  283.      * Gets the next sibling of this node.
  284.      * @return this node's next sibling node
  285.      * @see #getChild
  286.      * @see #getParent
  287.      */
  288.     public TreeNode getSibling()
  289.     {
  290.         return sibling;
  291.     }
  292.  
  293.     /**
  294.      * Sets whether this node is hidden.
  295.      * @param f <code>true</code> to make this node is hidden
  296.      * @see #isHidden
  297.      */
  298.     public void setHidden(boolean f)
  299.     {
  300.         hidden = f;
  301.  
  302.         if(treeView != null)
  303.         {
  304.             treeView.repaint(true);
  305.         }
  306.     }
  307.  
  308.     /**
  309.      * Gets whether this node is hidden.
  310.      * @return <code>true</code> if this node is hidden
  311.      * @see #setHidden
  312.      */
  313.     public boolean isHidden()
  314.     {
  315.         return (hidden);
  316.     }
  317.  
  318.     /**
  319.      * Determines whether any sibling nodes are visible.
  320.      * @return <code>true</code> if a sibling node is visible
  321.      * @see #isAChildVisible
  322.      */
  323.     public boolean isASiblingVisible()
  324.     {
  325.         TreeNode node;
  326.  
  327.         if(sibling == null)
  328.         {
  329.             return (false);
  330.         }
  331.  
  332.         if(!(sibling.isHidden()))
  333.         {
  334.             return (true);
  335.         }
  336.  
  337.         node = sibling;
  338.  
  339.         while(node.sibling != null)
  340.         {
  341.             node = node.sibling;
  342.  
  343.             if(!(node.isHidden()))
  344.             {
  345.                 return (true);
  346.             }
  347.         }
  348.  
  349.         return (false);
  350.     }
  351.  
  352.     /**
  353.      * Determines whether any child nodes are visible.
  354.      * @return <code>true</code> if a child node is visible
  355.      * @see #isASiblingVisible
  356.      */
  357.     public boolean isAChildVisible()
  358.     {
  359.         TreeNode node;
  360.  
  361.         if(child == null)
  362.         {
  363.             return (false);
  364.         }
  365.  
  366.         if(!(child.isHidden()))
  367.         {
  368.             return (true);
  369.         }
  370.  
  371.         if(child.sibling == null)
  372.         {
  373.             return (false);
  374.         }
  375.  
  376.         return (child.isASiblingVisible());
  377.     }
  378. }
  379.